<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    #########################################################################################
    # Programmer : nawattapon yoskamtorn						    #
    # Email	: contact@nawattapon.com						    #
    # Website	: http://www.nawattapon.com						    #
    #########################################################################################
    
    class Article_Type extends CI_Controller
    {
	function __construct()
	{
	    parent::__construct();
	    if( ! $this->session->userdata('lang')){
                $this->session->set_userdata('lang','english');
            }
            
            if( ! $this->session->userdata('admin_login')){
		redirect('/admin/login/' , 'refresh');
	    }
            
	    $this->template->set_template('backend');
            $this->lang->load('admin/main', $this->session->userdata('lang'));
	    
	    /* check privilege */
	    $action = $this->uri->segment(4) ? $this->uri->segment(4) : 'view';
	    if( ! check_privilege($this->session->userdata['admin']['user_group_id'] , 'video_articles' , $action)){
		show_error( $this->lang->line('permission_denied') );
	    }
            
            $this->load->model('article_type_model','article_type_model');
	}
	
	public function index()
	{
	    $order_by['title'] = 'ASC';
	    $this->article_type_model->set_order_by($order_by);
	    $data['article_type_list'] = $this->article_type_model->load_records(TRUE);
	    $this->template->write('title', $this->lang->line('back-end') ,FALSE);
	    $this->template->write_view('header','article_type_js');
	    $this->template->write_view('sidebar','master/sidebar_view');
	    $this->template->write_view('content','article_type_view',$data);
            $this->template->render();
	}
	
	public function add()
	{
            $this->load->library('form_validation');
	    $this->template->write('title', $this->lang->line('back-end') ,FALSE);
	    $this->template->write_view('header','article_type_add_js');
	    $this->template->write_view('sidebar','master/sidebar_view');
	    $this->template->write_view('content','article_type_add_view');
            $this->template->render();
	}
	
	public function add_process()
	{
	    $this->load->library('form_validation');
	    $this->form_validation->set_rules('title_en', 'lang:title', 'trim|xss_clean|required');
	    $this->form_validation->set_rules('title_th', 'lang:title', 'trim|xss_clean|required');
	    //$this->form_validation->set_rules('freindly_url', 'lang:freindly url', 'trim|xss_clean|required');
	    //$this->form_validation->set_rules('keyword', 'lang:keyword', 'trim|xss_clean|required');
	    //$this->form_validation->set_rules('description', 'lang:description', 'trim|xss_clean|required');
            
            if ($this->form_validation->run() == FALSE)
            {
                $this->add();
            }
            else
            {
		$this->load->library('ciupload');
		if($_FILES['img']){
		    $this->ciupload->Upload($_FILES['img']);
		    if ($this->ciupload->uploaded){
			$this->ciupload->image_resize = true;
			$this->ciupload->image_ratio_fill = true;
			$this->ciupload->image_x = 125;
			$this->ciupload->image_y = 125;
			$this->ciupload->Process(APPPATH.'../assets/image/article_type');
			
			if ($this->ciupload->processed){
			    $input['image'] = $this->ciupload->file_dst_name;
			}
		    }
		}
            }
	    
	    $this->load->helper('devlism');
		
	    $title['en'] = $this->input->post('title_en');
	    $title['th'] = $this->input->post('title_th');
	    $input['title'] = json_encode($title);
	    
	    //$input['url'] = generateSlug($this->input->post('freindly_url'));
	    //$input['keyword'] = $this->input->post('keyword');
	    //$input['description'] = $this->input->post('description');
	    
	    $this->article_type_model->insert_record($input);
	    
	    redirect('/admin/article/type/' , 'refresh');
	}
    }